<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class match extends Front_Controller {

	//--------------------------------------------------------------------

	public function __construct() 
	{
		parent::__construct();

		$this->load->library('form_validation');
		$this->form_validation->CI =& $this;
		$this->load->model('match_model', null, true);
		$this->load->model('matchodds/matchodds_model', null, true);
		$this->load->model('odds/odds_model', null, true);
		$this->lang->load('match');
		
		
	}
	
	//--------------------------------------------------------------------

	/*
		Method: index()
		
		Displays a list of form data.
	*/
	public function index() 
	{
		Template::set('records', $this->match_model->find_all());
		Template::render();
	}
	
	//--------------------------------------------------------------------

	public function getListByLeague($league_id = null) {
		$query_league_id = null;
		if (isset($league_id)) {
			$query_league_id = $league_id;
		} else {
			if (isset($_POST['league_id'])) { 
				$query_league_id = $_POST['league_id'];
			}	
		}
		
		$odds_list = array();
		$odds_list = $this->odds_model->find_all();
		if ($odds_list == false) {
			$odds_list = array();
		}
	
		$match_list = array();
		if (isset($query_league_id)) {
			$this->db->join('my_league_team', 'my_league_team.league_team_id = my_match.league_team_id1', 'left');
			$this->match_model->where('league_id', $query_league_id);
			$match_list = $this->match_model->find_all();			
			if ($match_list == false) {
				$match_list = array();
			}
		}
		
		$this->db->flush_cache();
		
		$match_data_list = array();
		for ($i = 0; $i < sizeof($match_list); $i++) {
			$match_data_item = new matchdataitem();
			$match_data_item->match = $match_list[$i];			
			$this->db->join('my_match_odds_ratio', 'my_match_odds_ratio.match_odds_id = my_match_odds.match_odds_id', 'right');
			$this->matchodds_model->where('match_id', $match_list[$i]->match_id);
			$match_odds_list = $this->matchodds_model->find_all();
			if ($match_odds_list == false) {
				$match_odds_list = array();
			}
			$match_data_item->match_odds_list = $match_odds_list;
			array_push($match_data_list, $match_data_item);
		}
		
		$matchData = new matchdata();
		$matchData->odds_list = $odds_list;
		$matchData->match_data_list = $match_data_list;
		
		$json_list = json_encode($matchData);
				
		Template::set(array('data' => $json_list));
		Template::render();
	}
}

class matchdataitem {
	public $match;
	public $match_odds_list = array();
}

class matchdata {
	public $odds_list = array();
	public $match_data_list = array();
}